package com.yuanjin.activity;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.yuanjin.R;
import com.yuanjin.util.MyTimeUtils;

public class Alarm_MainActivity extends Activity implements OnClickListener {
	private Button btn1, btn2, btn_cancel, btn3, btn4;
	private AlarmManager am;
	private long triggerAtMillis;
	private Intent intent;
	private PendingIntent op;
	private EditText et, etformat;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_activity_main);
		initview();

		// /////////////////////////
	}
	private void initview() {
		btn1 = (Button) findViewById(R.id.button1);
		btn1.setOnClickListener(this);
		btn2 = (Button) findViewById(R.id.button2);
		btn2.setOnClickListener(this);
		btn3 = (Button) findViewById(R.id.button3);
		btn3.setOnClickListener(this);
		btn_cancel = (Button) findViewById(R.id.button_cancel);
		btn_cancel.setOnClickListener(this);
		btn4 = (Button) findViewById(R.id.button4);
		btn4.setOnClickListener(this);
		et = (EditText) findViewById(R.id.edittext);
		etformat = (EditText) findViewById(R.id.edittextformat);

	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.button1 :// 定义一个闹钟提醒AlarmManager
				// 获取AlarmManager系统服务对象
				am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				// 闹钟提醒的时间（毫秒）
				triggerAtMillis = System.currentTimeMillis() + 3000;
				// 提醒后要触发的intent
				intent = new Intent(AlarmBroadcastReceiver.ACTION_ALARM);
				PendingIntent operation = PendingIntent.getBroadcast(this, 0,
						intent, PendingIntent.FLAG_UPDATE_CURRENT);
				am.set(AlarmManager.RTC, triggerAtMillis, operation);

				break;
			case R.id.button2 :// 设置重复提醒的闹钟
				am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				triggerAtMillis = System.currentTimeMillis() + 3000;
				// 闹钟重复提醒的时间间隔
				long intervalMillis = 4000;
				intent = new Intent(AlarmBroadcastReceiver.ACTION_ALARM);
				op = PendingIntent.getBroadcast(this, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);
				am.setRepeating(AlarmManager.RTC, triggerAtMillis,
						intervalMillis, op);
				break;
			case R.id.button_cancel :// 取消重复闹钟
				am.cancel(op);
				break;
			case R.id.button3 :// 实现一个闹钟实例
				am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				triggerAtMillis = System.currentTimeMillis() + 3000;
				intent = new Intent(this, AlarmActivity.class);
				op = PendingIntent.getActivity(this, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);
				am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, op);
				break;

			case R.id.button4 :
				String editTime = et.getText().toString();
				String timeFormat=etformat.getText().toString();
				long l = MyTimeUtils.getTimeFromString(editTime,timeFormat);

				am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
				triggerAtMillis = l;
				intent = new Intent(this, AlarmActivity.class);
				op = PendingIntent.getActivity(this, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);
				am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, op);
				Calendar d = Calendar.getInstance();
				//Calendar.get(Calendar.YEAR) - 1900
				//Calendar.get(Calendar.MONTH)
				//Calendar.get(Calendar.DAY_OF_WEEK)
				Log.e("test", "year="+d.get(Calendar.YEAR)+"month="+d.get(Calendar.MONTH)+"day="+d.get(Calendar.MONDAY)+"date="+d.get(Calendar.DATE));
				break;
			default :
				break;
		}
	}

}
